package com.puboot.common.util;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IDcard {

    static Scanner scanner = new Scanner(System.in);
    //主方法区
    public static void main(String[] args) {
        IDcard idCard = new IDcard();
        boolean flag = true;
        while (flag) {
            flag = idCard.menu();
        }
    }
    //主菜单
    private boolean menu() {
        boolean flag = true;
        System.out.println("************");
        System.out.println("1-根据身份证查询个人信息");
        System.out.println("2-退出");
        System.out.println("************");
        String order = scanner.nextLine();
        if (!is_rightorder(order)) {
            flag = false;
        }
        if (Integer.valueOf(order) == 1)
            operation();
        return flag;
    }
    //具体操作
    private void operation() {
        System.out.println("请输入正确身份证号");
        String id = scanner.nextLine();
        if (!check_id(id)) {
            String adress = openFile(id);// 获取当前地址
            if(adress ==null) {
                System.out.println("匹配不到地址");
                return ;
            }
            String birth = getBirth(id);// 获取出生日期
            String security = id.substring(14,16);//当地派出所代码
            String sex = getSex(id.substring(16,17));
            System.out.println("地址："+adress+"\n出生日期："+birth+"\n当地派出所代码："+security+"\n性别："+sex);
        }
    }
    //获取性别
    private String getSex(String sex) {
        if(Integer.valueOf(sex)%2==0) {
            return "女";
        }
        return "男";
    }
    //获取生日
    private String getBirth(String id) {
        String birth = id.substring(6,10)+"年"+id.substring(10,12)+"月"+id.substring(12,14)+"日";
        return birth;
    }

    // 检测是否是正确的命令
    private boolean is_rightorder(String order) {
        int o = Integer.valueOf(order);
        if ("".equals(order)) {
            return false;
        }
        if (o < 1 || o >= 2) {
            return false;
        }
        return true;
    }

    //检测id是否为真省份证号
//	将前面的身份证bai号码17位数分别乘以du不同的系数。从第一位zhi到第十七位的系数分别为：7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
//	2.将这17位数字和系数相乘的结果相加。
//	3.用加出来和除以11，看余数是多少？
    private boolean check_id(String id) {
        int sum = 0;
        boolean flag = true;
        String regex = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}(\\d{1}|x|X)$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(id);
        flag = m.matches();//身份证信息符合规范
        sum = Integer.valueOf(id.charAt(0))*7+Integer.valueOf(id.charAt(1))*9+Integer.valueOf(id.charAt(2))*10
                +Integer.valueOf(id.charAt(3))*5+Integer.valueOf(id.charAt(4))*8+Integer.valueOf(id.charAt(5))*4
                +Integer.valueOf(id.charAt(6))*2+Integer.valueOf(id.charAt(7))*1+Integer.valueOf(id.charAt(8))*6
                +Integer.valueOf(id.charAt(9))*3+Integer.valueOf(id.charAt(10))*7+Integer.valueOf(id.charAt(11))*9
                +Integer.valueOf(id.charAt(12))*10+Integer.valueOf(id.charAt(13))*5+Integer.valueOf(id.charAt(14))*8
                +Integer.valueOf(id.charAt(15))*4+Integer.valueOf(id.charAt(16))*2;
        sum = sum%11;
        char check = id.charAt(17);
        int a = -1;
        if(check == 'x') {
            a = 10;
        }else {
            a = Integer.valueOf(check);
        }
        if(sum != a) {		//判断校验值是否正确
            flag = false;
        }
        return flag;
    }

    // 打开文件匹配地址
    private String openFile(String id) {
        id = id.substring(0, 6);
        File file = new File("E:\\Downloads\\idcard\\idcard\\locationcode.txt");
        try {
//			FileReader fr = new FileReader(f);
            InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GBK");
            BufferedReader br = new BufferedReader(isr);
            String line;
            while ((line = br.readLine()) != null) {
                if (line.contains(id)) {
                    line = line.replaceAll("\\d*\\s", "");
//					System.out.println(line);
                    return line;
                }
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

